package com.auctionhaus

import grails.test.*

class CustomerControllerTests extends grails.test.ControllerUnitTestCase {
    protected void setUp() {
        super.setUp()
    }

    protected void tearDown() {
        super.tearDown()
    }

    void testSomething() {

    }

    // This checks out
    void testIndex(){
        controller.index()
        assertEquals "list", redirectArgs.action
    }


    void testRegistrationFailed(){
        mockRequest.method = 'POST'
        mockDomain(Customer)
        mockParams.emailAddress=""
        def model = controller.register()


        assertNull mockSession.customer
    }

    void testRegistrationDefaultsToUserRole(){
        mockRequest.method = 'POST'
        mockDomain(Customer, [new Customer(emailAddress:"fred@gmail.com", password:"password")])
        mockDomain(Customer)

        // create the customer params
        mockParams.emailAddress = "fred@gmail.com"
        mockParams.password = "Password"


        // verify that we have one customer
        def model = controller.register()
        assertEquals 1, model.size()

        assert model?.customerInstance
        def customer = model.customerInstance

        assertFalse customer.isAdminRole()
    }


    void testRegistrationNotUserButAdmin(){
        mockRequest.method = 'POST'
        mockDomain(Customer, [new Customer(emailAddress:"fred@gmail.com", password:"password")])
        mockDomain(Customer)

        // create the customer params
        mockParams.emailAddress = "fred@gmail.com"
        mockParams.password = "Password"
        mockParams.adminRole = true


        // verify that we have one customer
        def model = controller.register()
        assertEquals 1, model.size()

        assert model?.customerInstance
        def customer = model.customerInstance

        assertTrue customer.isAdminRole()
    }

    // Works 04/06/2011
    void testLoginCustomerNotFound(){
        mockRequest.method = 'POST'
        mockDomain(Customer)
        MockUtils.prepareForConstraintsTests(Customer)
        mockParams.emailAddress = "fred@gmail.com"
        mockParams.password = "password"
        controller.login()

        assertTrue controller.flash.message.startsWith('Customer with email Address')
        assertEquals "create", renderArgs.view
    }

    // Works 04/06/2011
    void testLoginPasswordInvalid(){
        mockRequest.method = 'POST'
        mockDomain(Customer, [new Customer(emailAddress:"fred@gmail.com", password:"password")])
        MockUtils.prepareForConstraintsTests(Customer)
        mockParams.emailAddress = "fred@gmail.com"
        mockParams.password = "invalid"
        controller.login()

        assertTrue controller.flash.message.startsWith('Invalid Password')
        assertEquals "create", renderArgs.view
    }

    // Works 04/06/2011 ----> Not sure if the controller should go back to list....
    void testSuccessfulLogin(){
        mockRequest.method = 'POST'
        mockDomain(Customer, [new Customer(emailAddress:"fred@gmail.com", password:"password", date:new Date())])

        mockParams.emailAddress = "fred@gmail.com"
        mockParams.password = "password"
        controller.login()

        assertEquals "myListings", redirectArgs.action
    }

    // This checks out
    void testCanCreateCustomer(){
        mockRequest.method = 'POST'
        mockDomain(Customer)

        // create the customer params
        mockParams.emailAddress = "GenericEmail@gmail.com"
        mockParams.password = "Password"
        mockParams.date = new Date(2011,3,10)

        // verify that we have one customer
        def model = controller.create()
        assertEquals 1, model.size()

        assert model?.customerInstance
        def customer = model.customerInstance

        // make sure we have no errors
        assertFalse 'no errors found', customer.hasErrors()
    }

    // This checks out
    void testCannotDeleteACustomerWithABid(){

        mockRequest.method = 'POST'
        def customer = new Customer()

        customer.bid = [new Bid()] //used to pass a collection

        mockDomain(Customer, [customer])

        // create the customer params id
        mockParams.id = 1

        controller.delete()

        assertEquals "show", redirectArgs.action
        assertTrue controller.flash.message.startsWith("Cannot delete a customer")
    }

    // This checks out
    void testCannotDeleteACustomerWithListing(){

        mockRequest.method = 'POST'
        def customer = new Customer()

        customer.listings = [new Listing()] //used to pass a collection

        mockDomain(Customer, [customer])

        // create the customer params id
        mockParams.id = 1

        controller.delete()

        assertEquals "show", redirectArgs.action
        assertTrue controller.flash.message.startsWith("Cannot delete a customer")
    }

    // This checks out
    void testCanDeleteACustomerWithoutListingsAndBids(){

        mockRequest.method = 'POST'
        def customer = new Customer()

        mockDomain(Customer, [customer])

        // create the customer params id
        mockParams.id = 1

        controller.delete()

        assertEquals "list", redirectArgs.action
        assertTrue controller.flash.message.startsWith("Deleting customer")
    }
}
